<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>measureService</title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>

    <script type="text/javascript">
        var map, url = "http://localhost:8090/iserver/services/map-Dubai/rest/maps/Dubai";
        map = L.map('map', {
            crs: L.CRS.EPSG4326,
            center: [25.08, 55.23],
            maxZoom: 18,
            zoom: 12
        });
        L.TileLayer.include({
            createTile: function (coords, done) {
                var tile = document.createElement('img');
                L.DomEvent.on(tile, 'load', L.Util.bind(this._tileOnLoad, this, done, tile));
                L.DomEvent.on(tile, 'error', L.Util.bind(this._tileOnError, this, done, tile));
                if (this.options.crossOrigin || this.options.crossOrigin === '') {
                    tile.crossOrigin = this.options.crossOrigin === true ? '' : this.options.crossOrigin;
                }
                tile.alt = '';
                tile.setAttribute('role', 'presentation');
                var xhr = new XMLHttpRequest();
                xhr.responseType = 'blob';
                xhr.addEventListener('loadend', function (evt) {
                    var data = this.response;
                    if (data !== undefined) {
                        tile.src = URL.createObjectURL(data);
                    } else {
                        tile.setState('error');
                    }

                });
                xhr.addEventListener('error', function () {
                    tile.setState('error');
                });
                xhr.open('GET', this.getTileUrl(coords));
                xhr.setRequestHeader('Accept', '*/*');
                xhr.setRequestHeader('X-HW-ID', 'userid');
                xhr.setRequestHeader('X-HW-APPKEY', 'username');
                xhr.send();
                return tile;
            }
        });
        
        L.supermap.tiledMapLayer(url).addTo(map);
        resultLayer = L.featureGroup().addTo(map);
        measureDistance();

        function measureDistance() {
            var polyline = L.polyline([[25.05, 55.23], [25, 55.23]], { color: "black" });
            resultLayer.addLayer(polyline);
            var marker1 = L.marker([25.02, 55.23]);
            resultLayer.addLayer(marker1);
            var distanceMeasureParam = new SuperMap.MeasureParameters(polyline);
            L.supermap
                .measureService(url, {
                    headers: {
                        "X-HW-ID": "userid",
                        "X-HW-APPKEY": "username"
                    }
                }).measureDistance(distanceMeasureParam, function (serviceResult) {
                    var content = resources.text_distance + serviceResult.result.distance + resources.msg_sqm;
                    marker1.bindPopup(content).openPopup(marker1.getCenter());
                });

        }
    </script>
</body>

</html>